﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;

namespace jacSocket
{
    public class JFormat
    {
        public string ToJson<T>(T obj)
        {
            var fm = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                fm.WriteObject(ms, obj);
                string strJSON = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                return strJSON;
            }
        }

        public T Deserialize<T>(string sJson) where T : class
        {
            var fm = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sJson));
            T obj = (T)fm.ReadObject(ms);
            ms.Close();
            return obj;
        }


    }
}
